package com.boss.salary.controller;


import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.OperrateLogs;
import com.boss.salary.entity.PersonInfo;
import com.boss.salary.service.IOperrateLogsService;
import com.boss.salary.service.IPersonInfoService;
import com.boss.salary.vo.PersonInfoVO;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.http.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/personInfoController", produces = MediaType.APPLICATION_JSON_VALUE)
public class PersonInfoController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(PersonInfoController.class);
    
    @Autowired
    private IPersonInfoService service;

    @Autowired
    private IOperrateLogsService iOperrateLogsService;

    /**
     * 查询 人员信息数据分页
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryPersonInfoPage")
    public ApiResultDTO queryPersonInfoPage(@RequestHeader("X-Token") String tokenid,
                                            @RequestParam(value = "page") String page,
                                            @RequestParam(value = "limit") String limit,
                                            @RequestParam(value = "agencyId", required = false) String agencyId,
                                            @RequestParam(value = "flag", required = false) String flag,
                                            @RequestParam(value = "meritPayCode", required = false) String meritPayCode,
                                            @RequestParam(value = "posCode", required = false) String posCode,
                                            @RequestParam(value = "grCode", required = false) String grCode,
                                            @RequestParam(value = "personName", required = false) String personName,
                                            @RequestParam(value = "idenNo", required = false) String idenNo,
                                            @RequestParam(value = "state", required = false) String state,
                                            @RequestParam(value = "personnelCode", required = false) String personnelCode
    ) {
        try {
            UserDTO userDTO = getUser(tokenid);
            Map<String, Object> queryParam = new HashMap<>(1);
            queryParam.put("flag", flag);
            queryParam.put("agencyId", agencyId);
            queryParam.put("meritPayCode", meritPayCode);
            queryParam.put("posCode", posCode);
            queryParam.put("grCode", grCode);
            queryParam.put("personName", personName);
            queryParam.put("idenNo", idenNo);
            queryParam.put("state", state);
            queryParam.put("mofDivCode",userDTO.getProvince());
            queryParam.put("personnelCode", personnelCode);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<PersonInfo> pageDTO = service.queryPersonInfoPage(paginationDTO, queryParam);
            return ApiResultDTO.success(pageDTO,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败！");
        }
    }
    @GetMapping("/queryOnePersonInfo")
    public ApiResultDTO queryOnePersonInfo(@RequestParam("personId") String personId){
        try {
            PersonInfoVO info = service.getOnePersonInfo(personId);
            return ApiResultDTO.success(info);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE,"查询失败！");
        }
    }

    /**
     * 查询 工资是否停发
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryStopPaySalary")
    public ApiResultDTO queryStopPaySalary(@RequestHeader("X-Token") String tokenid,
                                           @RequestParam(value = "page") String page,
                                            @RequestParam(value = "limit") String limit,
                                            @RequestParam(value = "flag", required = false) String flag,
                                            @RequestParam(value = "meritPayCode", required = false) String meritPayCode,
                                            @RequestParam(value = "posCode", required = false) String posCode,
                                            @RequestParam(value = "grCode", required = false) String grCode,
                                            @RequestParam(value = "personName", required = false) String personName,
                                            @RequestParam(value = "idenNo", required = false) String idenNo,
                                            @RequestParam(value = "isStopSalary", required = false) Integer isStopSalary) {
        try {
            UserDTO userDTO=getUser(tokenid);
            Map<String, Object> queryParam = new HashMap<>(1);
            queryParam.put("flag", flag);
            queryParam.put("meritPayCode", meritPayCode);
            queryParam.put("province",userDTO.getProvince());
            queryParam.put("posCode", posCode);
            queryParam.put("grCode", grCode);
            queryParam.put("personName", personName);
            queryParam.put("idenNo", idenNo);
            queryParam.put("isStopSalary", isStopSalary);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<PersonInfo> pageDTO = service.queryStopPaySalary(paginationDTO, queryParam,userDTO, tokenid);
            return ApiResultDTO.success(pageDTO,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE,"查询失败！");
        }
    }

    /**
     * 新增 人员信息数据
     * @param personInfo
     * @return
     */
    @PostMapping("/savePersonInfo")
    public ApiResultDTO savePersonInfo(@RequestBody PersonInfo personInfo) {
        try {
            service.savePersonInfo(personInfo);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error("新增 人员信息数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "保存失败！");
        }
    }

    /**
     * 更新 人员信息数据
     * @param personInfo
     * @return
     */
    @PostMapping("/updatePersonInfo")
    public ApiResultDTO updatePersonInfo(@RequestBody PersonInfo personInfo) {
        try {
            service.updatePersonInfo(personInfo);
            return ApiResultDTO.success(null, "更新成功");
        } catch (Exception e) {
            logger.error("更新 人员信息数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "更新失败！");
        }
    }

    /**
     * 批量更新 人员信息数据
     * @param personInfo
     * @return
     */
    @PostMapping("/updatePersonInfoList")
    public ApiResultDTO updatePersonInfoList(@RequestHeader("X-Token") String tokenid,
                                             @RequestBody List<PersonInfo> personInfo) {
        try {
            UserDTO userDTO = getUser(tokenid);

            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("单位信息编辑--机构人员维护--挂接科室");
            ArrayList<Object> personIds = new ArrayList<>();
            for (PersonInfo info : personInfo) {
                operrateLogs.setAgencyId(info.getAgencyId());
                personIds.add(info.getId());
            }
            String personid= org.apache.commons.lang.StringUtils.join(personIds,",");
            operrateLogs.setPersonId(personid);
            iOperrateLogsService.saveOperrateLogs(operrateLogs);
            
            service.updatePersonInfoList(personInfo);
            return ApiResultDTO.success(null, "更新成功");
        } catch (Exception e) {
            logger.error("批量更新 人员信息数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "更新失败！");
        }
    }



    /**
     * 删除 人员信息数据
     * @param id
     * @return
     */
    @DeleteMapping("/deletePersonInfo/{id}")
    public ApiResultDTO deletePersonInfo(@PathVariable("id") String id) {
        try {
            service.deletePersonInfo(id);
            return ApiResultDTO.success(null, "删除成功");
        } catch (Exception e) {
            logger.error("删除 人员信息数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "删除失败！");
        }
    }

    /**
     * 获取在职和离职人数
     * @return
     */
    @GetMapping("/getStateNum")
    public ApiResultDTO getStateNum(@RequestParam(value = "agencyId") String agencyId) {
        try {
            Map<String, Object> queryParam = new HashMap<>(1);
            queryParam.put("agencyId", agencyId);
            Map<String, Object> map = service.getStateNum(queryParam);
            return ApiResultDTO.success(map, null);
        } catch (Exception e) {
            logger.error("获取在职和离职人数数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败！");
        }
    }

}
